package io.renren.modules.PCguanli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.common.exception.RRException;
import io.renren.modules.PCguanli.dao.QuestionDao;
import io.renren.modules.PCguanli.entity.QuestionEntity;
import io.renren.modules.PCguanli.entity.TestQuestionEntity;
import io.renren.modules.PCguanli.service.TestQuestionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.PCguanli.dao.ExamineDao;
import io.renren.modules.PCguanli.entity.ExamineEntity;
import io.renren.modules.PCguanli.service.ExamineService;


@Service("examineService")
public class ExamineServiceImpl extends ServiceImpl<ExamineDao, ExamineEntity> implements ExamineService {

    @Autowired
    private ExamineDao examineDao;
    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private TestQuestionService testQuestionService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key=(String) params.get("key");
        LambdaQueryWrapper<ExamineEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(key), ExamineEntity::getNumber, key)
                .or().like(StringUtils.isNotEmpty(key), ExamineEntity::getCreationTime, key)
                .or().like(StringUtils.isNotEmpty(key), ExamineEntity::getExamTime, key);
        // 构建查询条件
        IPage<ExamineEntity> page = this.page(
                new Query<ExamineEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }


    @Override
    public void saveEntity(ExamineEntity examine){
        this.save(examine);
        saveBathTestQuestion(examine);
    }


    public void saveBathTestQuestion(ExamineEntity examine){
//        老师让我增加一个功能，点击一个按钮就可以批量增加20个试卷题目，以此形成一个完整的试卷:
//        我有三个表，一个是试卷，一个是组成试卷的题目，一个是题库。
//        它们之间的关联是：从题库中抽取20道题目组成试卷题目，其中10道必选题，10道随机选择的题目，一个试卷关联20个试卷题目
//        ExamineEntity examineEntity = examineDao.selectById(1);
        //生成的题目
        List<TestQuestionEntity> genralList = new ArrayList<>();

        List<QuestionEntity> questionEntityList = questionDao.selectList(new LambdaQueryWrapper<QuestionEntity>().eq(QuestionEntity::getIsuse, 0));
        List<QuestionEntity> isMustQuestionList = questionEntityList.stream().filter(e -> Objects.equals(e.getIsmust(), 1)).collect(Collectors.toList());
        // 打乱原始列表
        Collections.shuffle(isMustQuestionList);
        if (isMustQuestionList.size()<10) {
            throw new RRException("至少有10道必选题");
        }
        List<QuestionEntity> isMust10QuestionList = isMustQuestionList.subList(0, 10);
        System.out.println("10道必选题:"+isMust10QuestionList.toString());
        for (QuestionEntity questionEntity : isMust10QuestionList) {
            TestQuestionEntity testQuestionEntity = new TestQuestionEntity();
            testQuestionEntity.setQuestionNumber(questionEntity.getNumber());
            testQuestionEntity.setExamNumber(examine.getNumber());
//           todo  人工选择（是1否0）?
            testQuestionEntity.setIsPeopleChoose(0);
            genralList.add(testQuestionEntity);
        }


        List<QuestionEntity> noMustQuestionList = questionEntityList.stream().filter(e -> Objects.equals(e.getIsmust(), 0)).collect(Collectors.toList());
        // 打乱原始列表
        Collections.shuffle(noMustQuestionList);
        if (noMustQuestionList.size()<10) {
            throw new RRException("至少有10道随机选择的题目");
        }
        List<QuestionEntity> noMust10QuestionList = noMustQuestionList.subList(0, 10);
        System.out.println("10道随机选择的题目:"+noMust10QuestionList.toString());
        for (QuestionEntity questionEntity : noMust10QuestionList) {
            TestQuestionEntity testQuestionEntity = new TestQuestionEntity();
            testQuestionEntity.setQuestionNumber(questionEntity.getNumber());
            testQuestionEntity.setExamNumber(examine.getNumber());
//           todo  人工选择（是1否0）?
            testQuestionEntity.setIsPeopleChoose(0);
            genralList.add(testQuestionEntity);
        }

        //入库
        testQuestionService.saveBatch(genralList);

    }

}